<?php
class masterreel_model extends CI_Model{
	public function getLastInserted($table, $id) {
		$this->db->select_max($id);
		$Q = $this->db->get($table);
		$row = $Q->row_array();
		return $row[$id];
	}
	public function isEqual($val1,$val2){
		if($val1 == $val2){
			return true;
		}else{
			return false;
		}
	}
	public function getWorklist(){
		
		$query = $this->db->get('masterTemplate');
		 
		return json_encode($query->result());
	}
	
	public function keyInMasterReel($inputData,$inputAllData){
		 // check data in masterTemplate
		 $findMasterRealTable = array(
		 						"form_id"=>"1",
								"type_product"	=>$inputData["type_product"],
								"type_of"		=>$inputData["type_of"],
								"gram"			=>$inputData["gram"],
								//"size"			=>$inputData["size"],
								"grade"			=>$inputData["grade"],
								"shift"			=>$inputData["shift"],
								//"start_date"	=>$inputData[""],
								//"end_date"		=>$inputData[""],						 
								//"modify_date"	=>date("Y-m-d H:i:s"),
		 		
		 						);
		 $this->db->where($findMasterRealTable);
		 $query = $this->db->get('masterTemplate');
		if($query->num_rows() > 0){
			$rs = $query->result();
			$get_mt_id = $rs[0]->mt_id;   // หาค่า id เพื่อจะนำไปหา data ในตาราง dataTemplate

			$trasactionData = array(
					"form_id"=>"1",
					"mt_id"=>$get_mt_id,
					"type_product"	=>$inputData["type_product"],
					"type_of"		=>$inputData["type_of"],
					"gram"			=>$inputData["gram"],
					//"size"			=>$inputData["size"],
					"grade"			=>$inputData["grade"],
					"shift"			=>$inputData["shift"],
					"reel_no"		=> $inputData["reel_no"],
					"comment"		=>$inputData["comment"],
					"modify_date"	=>date("Y-m-d H:i:s"),
					"modify_user"	=>$this->session->userdata('user_id')
			);
			$this->db->insert("transactionData",$trasactionData);
			$myID = $this->getLastInserted('transactionData','t_id');
			$transactionStatus=1;
			foreach ($inputAllData as $key){
				$findData = array(
								//"form_id"=>"1",
								"mt_id"=>$get_mt_id,
								"data_name"=>$key["data_name"]
								 
						
						);
				$this->db->where($findData );
				$query = $this->db->get('dataTemplate');
				$dataTemplate = $query->result();
				$checkData = $dataTemplate[0];
				
				if(!(($checkData->value_start == 0) && ($checkData->value_end ==0))){
					
				
					if($this->isEqual($checkData->value_start,$checkData->value_end)){
						if($key["data_value"] == $checkData->value_start){
							$status= "1";
						}else{
							$status=  "0";
							
							$transactionStatus=0;  // ตรวจสอบว่าผิดเงื้อนไขหรือไม่ ถ้าผิดให้ไม่ผ่าน คือ status เป็น 0
						}
					}else{
						if(($key["data_value"] >= $checkData->value_start  ) && ($key["data_value"] <= $checkData->value_end ) ){
							$status=  "1";
						}else{
							$status=  "0";       // ตรวจสอบว่าผิดเงื้อนไขหรือไม่ ถ้าผิดให้ไม่ผ่าน คือ status เป็น 0
							$transactionStatus=0;
						}					
					}
				}else{
					$status=  "1";
				}
				$saveToDataValue = array(
									    "t_id"=>$myID,
										"data_name"=>$key["data_name"],
										"data_value"=>$key["data_value"],
										"status" => $status,
										"modify_time"=>date("Y-m-d H:i:s")
									);
				$this->db->insert("dataValue",$saveToDataValue);
				
				///
				if($transactionStatus == 1  ){
					//$transactionStatus=1;
					$outPut = "บันทึกข้อมูลเรียบร้อยแล้ว ผ่านการทดสอบ";
				
				}else{
					$outPut = "บันทึกข้อมูลเรียบร้อยแล้ว ไม่ผ่านการทดสอบ";
				}
				$updateStatus = array( "status"=>$transactionStatus);
				$this->db->where("t_id",$myID);
				$this->db->update("transactionData",$updateStatus);
				
			}
			
			/////////////////////////////////////////
			///  for send mail sms to next user
			 // get next flow and get next sms
			 
			 $this->workflow_model->checkNextStep($myID) ; // 1 masterreel
			
			// หา Profile
			
			 $inData=array( "mail_to" =>"ko@localhost", 
			 				"email_title" =>"You have 1 job in mailbox(Masterreel)",
			 				 "email_message" =>"<a href='http://127.0.0.1/work/project2012-3/index.php/login/index2/1/".
			 				base64_encode($myID)."'>Work </a>",
			 				 
			 			     "mobile"=>"0818666851"
			 			  );
			 $this->workflow_model->sendEmail($inData) ;
			// $this->workflow_model->sendSMS($inData);
			 
			
			///// end send email sms
			return $outPut;

		}else{
			
			return "Not Found Master Database";
		}
	//	echo $this->db->last_query();
		/////
	
	}
	
	
	
	public function checkMasterReel($inputData,$inputAllData){
	  
		// ตรวจสอบ ว่ามีในก Database ?
		 $this->db->where( $inputData);
		 $query = $this->db->get('masterTemplate');
		 $myID = $this->getLastInserted('masterTemplate','mt_id');
		   
		 if( $query->num_rows() == 0 ){
		 	$inputData["modify_date"]	= date("Y-m-d H:i:s");   // ค่าเวลาที่ modify database
		 	$this->db->insert("masterTemplate",$inputData);
		 	foreach($inputAllData as $key){
		 		$key["mt_id"]=  $myID+1;   // ค่าของ mt_id
		 		$key["status"]=  "1";   // ค่าเวลาที่ modify database
		 		$key["modify_time"]=  date("Y-m-d H:i:s");   // ค่าเวลาที่ modify database
		 		//return $key;
		 		$this->db->insert("dataTemplate",$key);
		 		//return true;
		 	}
		 	// all data
		 	// insert Data
		 	//$this->db->insert('dataTemplate',$inputAllData);
		 	 return "เพิ่มข้อมูล การตั้งค่าเรียบร้อยแล้ว!";
		 }else{
		  	return "ไม่สามารถเพิ่มข้อมูได้ มีข้อมูลอยู่แล้ว!!";
		 }
		
	}
		//- แสดงรายละเอียดของตาราง datastats -//
		public function masterreel_reel($input)
		{
			$this->db->order_by("TestID","desc"); 
			$this->db->where('TestID',$input['TestID']);
			$rs = $this->db->get("datastats");
			return $data['rs'] = $rs->result_array();
		}
		//- แสดงรายละเอียดของตาราง datavalue -//
		public function detail_masterreel1($t_id)
		{
			$this->db->where('t_id',$t_id);
			$rs=$this->db->get("transactionData");
			return $data['rs1'] = $rs->result_array();
		}
		public function detail_masterreel2($t_id)
		{
			$this->db->where('t_id',$t_id);
			$rs=$this->db->get("dataValue");
			return $data['rs2'] = $rs->result_array();
		}
		//- แสดงรายละเอียดของตาราง datatemplate -//
		public function information_masterreel1($mt_id)
		{
			$this->db->where('mt_id',$mt_id);
			$rs=$this->db->get("masterTemplate");
			return $data['rs1'] = $rs->result_array();
		}
		public function information_masterreel2($mt_id)
		{
			$this->db->where('mt_id',$mt_id);
			$rs=$this->db->get("dataTemplate");
			return $data['rs2'] = $rs->result_array();
		}
		//--//
		public function list_masterreel($t_id)
		{   
			if($this->session->userdata('admin')==0 ){ // ตรวจสอบว่า ถ้าไม่ใช่ admin ให้แสดงตาม id user 
				$this->db->where('modify_user',$this->session->userdata('user_id'));
			}
			$this->db->order_by('t_id','DESC');
			$query = $this->db->get($t_id);
			//return $data['rs'] = $rs->result_array();

			$i=1;
			$out_data = array(); // กำหนดค่าใหม่ให้กับ array
			foreach($query->result_array() as $key ){
				// echo "<pre>";
				$key["form_name"] = $this->application_model->checkFormName($key["form_id"]);
				$key["order_id"] = $i;
				$key["user_keyin"] = $this->user_model->getFullName($key["modify_user"]);
				 if($key["status"] == 0 ){
					$key["status_text"] = "NC";
				}else{
					$key["status_text"] = "Pass";
				}
				$out_data[] = $key;
				//  print_r($key);
				$i++;
			}
			return $out_data;

		}
		
		public function list_information($t_id)
		{
			if($this->session->userdata('admin')==0 ){ // ตรวจสอบว่า ถ้าไม่ใช่ admin ให้แสดงตาม id user
				$this->db->where('modify_user',$this->session->userdata('user_id'));
			}
			$this->db->order_by('t_id','DESC');
			$query = $this->db->get($t_id);
			//return $data['rs'] = $rs->result_array();
		
			$i=1;
			$out_data = array(); // กำหนดค่าใหม่ให้กับ array
			foreach($query->result_array() as $key ){
				// echo "<pre>";
				$key["form_name"] = $this->application_model->checkFormName($key["form_id"]);
				$key["order_id"] = $i;
				$key["user_keyin"] = $this->user_model->getFullName($key["modify_user"]);
				if($key["status"] == 0 ){
					$key["status_text"] = "Not Completed";
				}else{
					$key["status_text"] = "Completed";
				}
				$out_data[] = $key;
				//  print_r($key);
				$i++;
			}
			return $out_data;
		
		}
}
?>
